<?php

namespace App\Models;

use App\Helpers\Constant;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Modules\Car\Entities\CarCategory;
use Modules\Car\Entities\RecognizeDrivingLicense;

class Car extends Model
{
    use HasFactory;
    protected $guarded = [];
    protected $appends = [
        'gear_car_num',//车辆数量GearCarNum
        'empty_number'
    ];
    //车辆数量
    public function getGearCarNumAttribute()
    {
        return RecognizeDrivingLicense::query()->where('car_series_id',$this->attributes['id'])->count();
    }
    public function calendar(){
        return $this->hasMany(CarCalendar::class,'car_id');
    }
    public function setNameAttribute($value)
    {
        return $this->attributes['name'] = htmlspecialchars($value);
    }
    public function getPicAttribute($value){
        return $value;
        //$store = str_replace('/storage','',$value);
      //  $zc =  str_replace('zc.','page.',$store);
      //  $ywbtzc =  str_replace('ywbtpage.','ywbtzc.',$zc);
      //  $cn =  str_replace('cn/','com/',$ywbtzc);
       // return $cn;
    }
    public function cate(){
        return $this->belongsTo(CarCategory::class,'category_id');
    }
//    public function drive(){
//        return $this->hasOne(RecognizeDrivingLicense::class,'car_id');
//    }
    public function drive(){
        return $this->hasMany(RecognizeDrivingLicense::class,'car_series_id');
    }
    // 获取当前车系数量
    public function getEmptyNumberAttribute(){
        return RecognizeDrivingLicense::query()
            ->where('car_series_id',$this->attributes['id'])
            ->where('is_used',Constant::CAR_EMPTY)
            ->where('status',Constant::CAR_USE_STATUS_EMPTY)
            ->count();
    }

}
